<?php

/**
 * Description of mobilehotword
 *
 * @author libaoling
 * @time 2014-8-19
 */
class MobilehotkeywordApp extends BackendApp
{
    var $_keyword_mod;

    function __construct()
    {
        $this->HotkeywordApp();
    }

    function HotkeywordApp()
    {
        parent::BackendApp();

        $this->_keyword_mod =& m('mobilehotkeyword');
    }

    function index()
    {
		$conditions = $this->_get_query_conditions(array(array(
		'field' => 'keyword_name',       
		'equal' => 'LIKE',
		'name'  => 'search_name',
		),array(
		'field' => 'recom_id',
		'equal' => '=',
		'type'  => 'numeric',
		)
		));
        $page   =   $this->_get_page(10);    //获取分页信息
        $hotkeyword = $this->_keyword_mod->find(array(
            'conditions'    => '1=1 ' . $conditions,
            'limit'         => $page['limit'],  //获取当前页的数据
            'order'         => 'sort_order,keyword_id desc',
            'count'         => true             //允许统计
        )); 
        $page['item_count'] = $this->_keyword_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->import_resource(array('script' => 'inline_edit.js'));
		$this->assign('recom_list', $this->_get_recom_list());//推荐类型
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
        $this->assign('hotkeyword', $hotkeyword); 
        $this->display('mobilehotkeyword.index.html');
    }

    function add()
    {
        if (!IS_POST)
        {
            $hotkeyword = array(
            'sort_order'    => 255,
            'link'          => 'http://',
            );
			
            $this->assign('hotkeyword' , $hotkeyword);
            $this->assign('recom_list', $this->_get_recom_list());//推荐类型
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('mobilehotkeyword.form.html');
        }
        else
        {

            $data = array();
            $data['keyword_name']      =   trim($_POST['keyword_name']);
            $data['recom_id']     =   intval($_POST['recom_id']);
            $data['link']  =   trim($_POST['link']);
            $data['sort_order']      =   intval($_POST['sort_order']);
            if (!$this->_keyword_mod->add($data))
            {
                $this->show_warning($this->_keyword_mod->get_error());

                return;
            }
            $this->_keyword_mod->clear_template_cache();
            $this->show_message('添加成功',
                'back_list',    'index.php?app=mobilehotkeyword',
                '继续添加', 'index.php?app=mobilehotkeyword&amp;act=add'
            );
        }
    }

    function edit()
    {	
        $keyword_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$keyword_id)
        {
            $this->show_warning('没有相关的数据');

            return;
        }
        if (!IS_POST)
        {
            $find_data     = $this->_keyword_mod->find($keyword_id);
            if (empty($find_data))
            {
                $this->show_warning('没有相关的数据');

                return;
            }
            $hotkeyword    =   current($find_data);
			$this->assign('hotkeyword' , $hotkeyword);
            $this->assign('recom_list', $this->_get_recom_list());//推荐类型
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('mobilehotkeyword.form.html');
        }
        else
        {
            $data = array();
            $data['keyword_name']      =   trim($_POST['keyword_name']);
            $data['recom_id']     =   intval($_POST['recom_id']);
            $data['link']  =   trim($_POST['link']);
            $data['sort_order']      =   intval($_POST['sort_order']);            
            $this->_keyword_mod->edit($keyword_id, $data);
            if ($this->_keyword_mod->has_error())    //有错误
            {
                $this->show_warning($this->_keyword_mod->get_error());

                return;
            }
            $this->_keyword_mod->clear_template_cache();
            $this->show_message('编辑成功',
                'back_list',     'index.php?app=mobilehotkeyword',
                '再次编辑', 'index.php?app=mobilehotkeyword&amp;act=edit&amp;id=' . $keyword_id);
        }
    }

    //异步修改数据
   function ajax_col()  
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();

       if (in_array($column ,array('keyword_name', 'sort_order')))
       {
           $data[$column] = $value;
           $this->_keyword_mod->edit($id, $data);
           if(!$this->_keyword_mod->has_error())
           {
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }

    function drop()
    {
        $keyword_ids = isset($_GET['id']) ? trim($_GET['id']) : 0;
        if (!$keyword_ids)
        {
            $this->show_warning('请选择要删除的数据');

            return;
        }
        $keyword_ids = explode(',', $keyword_ids);//获取一个类似array(1, 2, 3)的数组
        if (!$this->_keyword_mod->drop($keyword_ids))    //删除
        {
            $this->show_warning($this->_keyword_mod->get_error());

            return;
        }
        $this->_keyword_mod->clear_template_cache();
        $this->show_message('删除成功');
    }

     /* 更新排序 */
    function update_order()
    {
        if (empty($_GET['id']))
        {
            $this->show_warning('Hacking Attempt');
            return;
        }

        $ids = explode(',', $_GET['id']);
        $sort_orders = explode(',', $_GET['sort_order']);
        foreach ($ids as $key => $id)
        {
            $this->_keyword_mod->edit($id, array('sort_order' => $sort_orders[$key]));
        }
        $this->_keyword_mod->clear_template_cache();
        $this->show_message('更新排序成功');
    }
    
     /* 获取推荐类型 */
    function _get_recom_list()
    {
        $arr = array();
        $recom_mod =& m('recommend'); 
        $recom_list = $recom_mod->find(array(
            'fields' => 'recom_id,recom_name',
            'order'         => 'recom_id desc',
        ));
        foreach($recom_list as $val)
        {
            $arr[$val['recom_id']] = $val['recom_name'];
        }
        
        return $arr;
    }


}

?>